<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
  },
  data() {
    return {
      appWindow: window,
      form: {
        //---  规则设置
        minDeductUnit: "", //最小抵扣单位
        deductRuleAmount: "", //抵扣金额
        defaultOpenFlag: 0, //使用状态
        //---

        // ===  抵用设置
        isOpenOrderAmount: "", // 是否抵扣商品金额
        isOpenFreight: 0, //是否抵扣运费
        productType: 1, // 什么商品参与

        isOpenMaxUse: 0, //是否开启最多使用
        isOpenMinUse: 0, //是否开启最少使用
        isOpenOrderAmountProportion: 0, //是否开启抵扣项目金额比例
        isOpenReturn: 0, //是否开启按比例返回
        conditionNode: 1, //结算的方式

        maxUseNumber: "", //最多使用数量
        minUseNumber: "", //最少使用数量
        orderAmountProportion: "", //抵扣比例
        returnPercentage: "", //返还比例
      },

      goodsIds: [],

      apiUrl: {
        detail: "mall-site-portal/member-integral-type/getItemSetting",
        update: "mall-site-portal/member-integral-type/updateSetting",
      },

      assetName: "", //资产类型名称

      load: true,
    };
  },

  methods: {
    // 保存设置
    async submit() {
      if (!(await this.validate())) {
        return;
      }

      const { isOpenOrderAmount, productType } = this.form;

      if (isOpenOrderAmount === 1) {
        if (productType !== 1 && this.goodsIds.length === 0) {
          this.$vFN.Tools.messageError("至少选择一项商品");
          return;
        }
      }
      this.load = true;

      for (let key in this.form) {
        this.form[key] = Number(this.form[key]);
      }

      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.update,
          data: {
            ...this.form,
            productIdLst: this.goodsIds,
            typeId: this.typeId, //从home 传来
          },
        },
        window
      );

      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.mixinEdit_editVisible = false;
      }
      this.load = false;
    },

    init() {
      this.mixinEdit_editVisible = true;
      this.getSetting();
      this.assetName = this.typeName; //从home传来
    },

    // 获取设置信息
    async getSetting() {
      this.load = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
          data: {
            typeId: this.typeId, //从home 传来
          },
        },
        window
      );

      if (res._ok) {
        console.log(res.data);

        const {
          minDeductUnit,
          defaultOpenFlag,
          isOpenOrderAmount,
          isOpenFreight,
          productType,
          isOpenMaxUse,
          isOpenMinUse,
          isOpenReturn,
          isOpenOrderAmountProportion,
          conditionNode,
          maxUseNumber,
          minUseNumber,
          orderAmountProportion,
          returnPercentage,
          productIdLst,
        } = res.data;

        this.form = {
          minDeductUnit,
          deductRuleAmount: 1, //写死
          defaultOpenFlag: defaultOpenFlag || 0,
          isOpenOrderAmount: isOpenOrderAmount || 0,
          isOpenFreight: isOpenFreight || 0,
          productType: productType || 1,
          isOpenMaxUse: isOpenMaxUse || 0,
          isOpenMinUse: isOpenMinUse || 0,
          isOpenReturn: isOpenReturn || 0,
          isOpenOrderAmountProportion: isOpenOrderAmountProportion || 0,
          conditionNode: conditionNode || 1,
          maxUseNumber,
          minUseNumber,
          orderAmountProportion,
          returnPercentage,
        };

        // 商品数组
        this.goodsIds = productIdLst;

        this.load = false;

        console.log(this.form);
      }
    },

    //  验证方法开始  ===========
    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    // 抵扣项目金额比例
    validator(rule, value, callback) {
      value = +value;

      console.error(value);
      console.error(typeof value);

      console.log();
      if (this.form.isOpenOrderAmountProportion == 0) {
        callback();
      } else if (value > 100) {
        callback(new Error("最大比例为100%"));
      } else {
        this.$vFN.Tools.validateMoney(rule, value, callback);
      }
    },

    // 比例返还
    validator2(rule, value, callback) {
      console.error(value);

      if (this.form.isOpenReturn == 0) {
        callback();
      } else if (value > 100) {
        callback(new Error("最大比例为100%"));
      } else {
        this.$vFN.Tools.validateMoney(rule, value, callback);
      }
    },

    // 抵扣规则
    validatorMinDeductUnit(rule, value, callback) {
      if (value === "") {
        callback(new Error("不能为空"));
      } else {
        this.$vFN.Tools.validateMoney(rule, value, callback);
      }
    },

    // 抵扣数量最少使用
    validatorMinUseNumber(rule, value, callback) {
      const { defaultOpenFlag, isOpenMinUse } = this.form;

      if (defaultOpenFlag === 1 && isOpenMinUse === 1) {
        if (value === "") {
          callback(new Error("不能为空"));
        } else {
          this.$vFN.Tools.validateStock(rule, value, callback);
        }
      } else if (defaultOpenFlag === 1 && isOpenMinUse === 0) {
        this.form.minUseNumber = "";
        callback();
      } else {
        callback();
      }
    },

    // 抵扣数量最多使用
    validatorMaxUseNumber(rule, value, callback) {
      const { defaultOpenFlag, isOpenMaxUse } = this.form;

      if (defaultOpenFlag === 1 && isOpenMaxUse === 1) {
        if (value === "") {
          callback(new Error("不能为空"));
        } else {
          this.$vFN.Tools.validateStock(rule, value, callback);
        }
      } else if (defaultOpenFlag === 1 && isOpenMaxUse === 0) {
        this.form.maxUseNumber = "";
        callback();
      } else {
        callback();
      }
    },

    // 验证方法结束 =========
  },

  mounted() {
    this.init();
  },
};
</script>
